<template>
	<view class="content">
		<image class="banner" src="/static/image/banner.png"></image>
		<view class="content-bg">
			<view class="content-box">
				<view class="entry-box">
					<view class="entry-item" @click="navigateToBallroomIntro">
						<image class="entry-icon" src="/static/image/icon_pp.png"></image>
						<text>球室介绍</text>
					</view>
					<view class="entry-item" @click="openAppointmentModal">
						<image class="entry-icon" src="/static/image/icon_book.png"></image>
						<text>预约须知</text>
					</view>
					<view class="entry-item" @click="navigateToAppointmentList">
						<image class="entry-icon" src="/static/image/icon_appointment.png"></image>
						<text>我的预约</text>
					</view>
				</view>
				<view class="info">
					<view class="info-item place">
						<image class="place-icon" src="/static/image/icon_place.png"></image>
						<text>场馆地址：何家湖校区图书馆1楼内</text>
					</view>
					<view class="info-item appointment-time">
						<image class="place-icon" src="/static/image/icon_time.png"></image>
						<text>预约时间：<text class="time-color">{{ appointment_time }}</text></text>
					</view>
				</view>
				<view class="list-title">
					<view class="title-left">球桌列表</view>
					<view class="title-right" @click="navigateToList">
						<text>查看更多</text>
						<image class="icon" src="/static/image/arrow_right.png"></image>
					</view>
				</view>
				<view class="list-box">
					<view class="list-item" v-for="item in table_Data.slice(0, 2)" :key="item.id">
						<image :src="item.imageUrl"></image>
						<view class="item-info">
							<text>名称:{{ item.name }}</text>
							<text>价格:￥{{ item.price }} 元/小时</text>
							<view @click="goToReservation(item.id)" v-if="userConfig.isAdmin">预定</view>
						</view>
					</view>

				</view>
			</view>
		</view>
		<!-- 预约须知弹窗 -->
		<view class="modal" v-if="showModal" @touchmove.stop.prevent>
			<view class="modal-content">
				<view class="modal-header">
					<text class="modal-title">预约须知</text>
					<view class="close-btn" @click="closeAppointmentModal">×</view>
				</view>
				<view class="modal-body">
					<view class="notice-content">
						<text class="notice-text">1. 本场馆开放时间：每天 8:00-22:00</text>
						<text class="notice-text">2. 预约需提前1小时以上，可预约未来7天内的场地</text>
						<text class="notice-text">3. 每次预约时长不超过2小时</text>
						<text class="notice-text">4. 请按时到场使用，如需取消请提前30分钟</text>
						<text class="notice-text">5. 使用时请爱护设施，损坏需照价赔偿</text>
						<text class="notice-text">6. 禁止携带食物进入场地，保持场地清洁</text>
						<text class="notice-text">7. 请穿着合适的运动服装和鞋子</text>
					</view>
				</view>
				<view class="modal-footer">
					<button class="confirm-btn" @click="closeAppointmentModal">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getUserInfo
	} from '@/services/base.js'
	import {
		getProductList
	} from '@/services/product.js';
	const appointment_time = ref('19:00 - 21:00');
	const showModal = ref(false);
	const userConfig = uni.getStorageSync('userConfig')
	const table_Data = ref([])

	const getList = async () => {
		const list = await getProductList()
		console.log(list)
		table_Data.value = list.data?.list ?? []
	}

	getList()
	onLoad(async () => {
		const userInfo = await getUserInfo()
		if (userInfo.code == 200) {
			uni.setStorageSync('userConfig', {
				...userConfig,
				...userInfo.data
			})
		}
	})
	// 跳转到列表页
	const navigateToList = () => {
		uni.switchTab({
			url: '/pages/list/list'
		})
	}

	// 跳转到球室介绍页面
	const navigateToBallroomIntro = () => {
		uni.navigateTo({
			url: '/pages/index/ballroomIntroduction'
		})
	}

	// 打开预约须知弹窗
	const openAppointmentModal = () => {
		showModal.value = true;
		// 禁止背景滚动
		document.body.style.overflow = 'hidden';
	}

	// 关闭预约须知弹窗
	const closeAppointmentModal = () => {
		showModal.value = false;
		// 恢复背景滚动
		document.body.style.overflow = '';
	}
	const goToReservation = (tableId) => {
		uni.navigateTo({
			url: `/pages/list/appointmentDetails?tableId=${tableId}`
		})
	}
	const navigateToAppointmentList = () => {
		uni.navigateTo({
			url: '/pages/mine/myOrder'
		})
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #f5f5f5;
		min-height: 100vh;

		.banner {
			width: 100%;
			height: 160px;
			// object-fit: contain;
		}

		.content-bg {
			width: 100%;

			.content-box {
				padding: 0 15px;
				padding-top: 20px;

				.entry-box {
					background-color: #fff;
					border-radius: 15px;
					box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.25);
					display: flex;
					justify-content: space-around;
					padding: 12px;

					.entry-item {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.entry-icon {
							width: 35px;
							height: 35px;
							margin-bottom: 5px;
						}
					}
				}

				.list-title {
					margin: 20px 0;
					display: flex;
					justify-content: space-between;

					.title-left {
						display: flex;
						align-items: center;

						&::before {
							content: '';
							display: inline-block;
							width: 4px;
							height: 16px;
							background: rgba(81, 89, 245, 1);
							border-radius: 20px;
							margin-right: 5px;
						}
					}

					.title-right {
						display: flex;
						align-items: center;

						image {
							width: 15px;
							height: 15px;
							margin-left: 5px;
						}
					}
				}

				.list-box {
					.list-item {
						background-color: #fff;
						border-radius: 10px;
						display: flex;
						flex-direction: row;
						padding: 15px;
						margin-bottom: 15px;

						image {
							width: 100px;
							height: 100px;
							border-radius: 8px;
							margin-right: 15px;
						}

						.item-info {
							flex: 1;
							display: flex;
							flex-direction: column;
							justify-content: space-between;

							text:nth-child(1) {
								font-size: 18px;
								font-weight: bold;
							}

							text:nth-child(2) {
								color: #ff6600;
								font-size: 16px;
							}

							text:nth-child(3) {
								font-size: 12px;
							}

							view {
								align-self: flex-end;
								background-color: #3b82f6;
								color: white;
								padding: 5px 15px;
								border-radius: 20px;
								cursor: pointer;
							}
						}
					}
				}

				.info {
					margin: 20px 0;

					.info-item {
						background-color: #fff;
						border-radius: 2px;
						// display: flex;
						// align-items: center;
						margin-bottom: 10px;
						padding: 10px 15px;
						display: flex;
						align-content: center;

						image {
							margin-right: 10px;
						}

						&.place {
							image {
								width: 23px;
								height: 23px;
								margin-right: 8px;
							}
						}

						&.appointment-time {
							image {
								width: 20px;
								height: 20px;
							}

							.time-color {
								color: rgb(249, 156, 17);

							}
						}

					}

				}
			}

		}
	}

	// 弹窗样式
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;

		.modal-content {
			background-color: #fff;
			border-radius: 10px;
			width: 70%;
			max-width: 400px;
			padding: 20px;

			.modal-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20px;
				padding-bottom: 10px;
				border-bottom: 1px solid #eee;

				.modal-title {
					font-size: 18px;
					font-weight: bold;
				}

				.close-btn {
					font-size: 24px;
					cursor: pointer;
				}
			}

			.modal-body {
				.notice-content {
					.notice-text {
						display: block;
					}
				}
			}

			.modal-footer {
				display: flex;
				justify-content: space-between;
				margin-top: 20px;

				button {
					flex: 1;
					padding: 5px 10px;
					border-radius: 5px;
					border: none;
					cursor: pointer;

					&.cancel-btn {
						background-color: #f5f5f5;
						color: #333;
						margin-right: 10px;
					}

					&.confirm-btn {
						background-color: #3b82f6;
						color: white;
						margin-left: 10px;
					}
				}
			}
		}
	}
</style>